#include "AT24C02.h"
#include "tcp_demo.h"
#include "usart.h"
#define  use_size  32  //使用at24c02的空间
uint8_t  memroy[use_size];

//带写入块检测的数据读写一个区块顶多能写入8位数据，多个数据读写需要多次写入

//从at24 读取函数
uint8_t AT24C02_READ(uint8_t *DATA ,uint16_t ADD,uint16_t  LENGTH)
{
	     //超过容量
	     if((ADD+LENGTH)>CAPACITY_SIZE){ return 0;}
			 
			 int length=LENGTH;
			 uint16_t nowadd=ADD;
			 while(length>0)
			 {
         if(HAL_I2C_Mem_Read(&hi2c1,DEV_ADDR_R,nowadd,ADDR_BYTE_NUM  ,DATA,length>PAGE_SIZE?PAGE_SIZE:length,60000)!=HAL_OK)
			   {
			      return 0; 
			   }
				 DATA=DATA+PAGE_SIZE;
				 length-=PAGE_SIZE;
				 nowadd+=8;
		   }
			 return 1;
}

//从at24写入函数
uint8_t AT24C02_WRITE(uint8_t *DATA ,uint16_t ADD,uint16_t  LENGTH)
{
	     //超过容量
	     if((ADD+LENGTH)>CAPACITY_SIZE){ return 0;}
			 
			 int length=LENGTH;
			 uint16_t nowadd=ADD;
			 uint16_t empty_size=length>PAGE_SIZE?PAGE_SIZE:length;
			 while(length>0)
			 {
					 if(HAL_I2C_Mem_Write(&hi2c1,DEV_ADDR_W,nowadd,ADDR_BYTE_NUM  ,DATA,empty_size,60000)!=HAL_OK)
					 {
								 return 0;
					 }
					 HAL_Delay(10);     //wait at24c512 wirte data
					 DATA=DATA+PAGE_SIZE;
				   length-=PAGE_SIZE;
				   nowadd+=8;
			}
			return 1;
}




	

 http_config http_at24_data_test;

void AT24C02_INIT()
{
   HAL_GPIO_WritePin(WP_LOCK_GPIO_Port,WP_LOCK_Pin,GPIO_PIN_RESET); 
	
	 if(HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin)==0)
	 {
	    AT24C02_save_data();//保存数据到AT24C02
		 //上电前按住按钮可以复位
	 }
	
	
	 AT24C02_load_data();//从AT24C02加载数据
   //AT24C02_WRITE((uint8_t *)&http_at24_data,0,sizeof(http_at24_data));
	 //AT24C02_READ((uint8_t *)&http_at24_data_test,0,sizeof(http_at24_data));
}


void AT24C02_load_data(void)//从AT24C02加载数据
{
	  uint16_t CRC_CHECK;
	  //从at24加载数据和校验码
    AT24C02_READ((uint8_t *)&http_at24_data_test,0,sizeof(http_at24_data)); 
	  AT24C02_READ((uint8_t *)&CRC_CHECK,sizeof(http_at24_data),sizeof(CRC_CHECK)); 

	  
	  if(CRC_CHECK==CRC_calculate_crc16(0xffff,0x8005,0,1,1,(uint8_t *)&http_at24_data_test,sizeof(http_at24_data)))
		{
		   //使用加载数据
			 memcpy((uint8_t *)&http_at24_data,(uint8_t *)&http_at24_data_test,sizeof(http_at24_data));
       local_port=http_at24_data.IP_SOCKET[0];                             	  /* 定义本地端口                 */
       local_port2=http_at24_data.IP_SOCKET[1];                              	  /* 定义本地端口                 */


/*定义MAC地址,如果多块W5500网络适配板在同一现场工作，请使用不同的MAC地址*/
       mac[2]=http_at24_data.IP[0];
			 mac[3]=http_at24_data.IP[1];
			 mac[4]=http_at24_data.IP[2];
			 mac[5]=http_at24_data.IP[3];

       local_ip[0]=http_at24_data.IP[0];										/*定义W5500默认IP地址*/
			 local_ip[1]=http_at24_data.IP[1];										/*定义W5500默认IP地址*/
			 local_ip[2]=http_at24_data.IP[2];										/*定义W5500默认IP地址*/
			 local_ip[3]=http_at24_data.IP[3];										/*定义W5500默认IP地址*/
													                                  /*定义W5500默认子网掩码*/
			
       gateway[0]=http_at24_data.IP[0];											              /*定义W5500默认网关*/
		   gateway[1]=http_at24_data.IP[1];											              /*定义W5500默认网关*/
			 gateway[2]=http_at24_data.IP[2];											              /*定义W5500默认网关*/
			 
       ///////////////////////////
			 huart2_set.Init.BaudRate = http_at24_data.BOUND[1];
			 huart2_set.Init.WordLength =http_at24_data.data_len[1]==1?UART_WORDLENGTH_8B:UART_WORDLENGTH_9B;
			 huart2_set.Init.StopBits = http_at24_data.data_stop[1]==0?UART_STOPBITS_1:UART_STOPBITS_2;
			 if(http_at24_data.data_check[1]==0)
			 {
				  //stm32默认添加校验位时数据长度还是8，使用奇偶校验要把数据位改成9
			    huart2_set.Init.Parity = UART_PARITY_ODD;
				  huart2_set.Init.WordLength =UART_WORDLENGTH_9B;
			 }
			 else if(http_at24_data.data_check[1]==1)
			 {
			    huart2_set.Init.Parity = UART_PARITY_EVEN;
				  huart2_set.Init.WordLength =UART_WORDLENGTH_9B;
			 }
			 else
			 {
			    huart2_set.Init.Parity = UART_PARITY_NONE;
			 }
			 /////////////////////////
			 huart1_set.Init.BaudRate = http_at24_data.BOUND[0];
			 huart1_set.Init.WordLength =http_at24_data.data_len[0]==1?UART_WORDLENGTH_8B:UART_WORDLENGTH_9B;
			 huart1_set.Init.StopBits = http_at24_data.data_stop[0]==0?UART_STOPBITS_1:UART_STOPBITS_2;
			 if(http_at24_data.data_check[0]==0)
			 {
			    huart1_set.Init.Parity = UART_PARITY_ODD;
				  huart1_set.Init.WordLength =UART_WORDLENGTH_9B;
			 }
			 else if(http_at24_data.data_check[0]==1)
			 {
			    huart1_set.Init.Parity = UART_PARITY_EVEN;
				  huart1_set.Init.WordLength =UART_WORDLENGTH_9B;
			 }
			 else
			 {
			    huart1_set.Init.Parity = UART_PARITY_NONE;
			 }
		}
		else
		{
			 
			//校验没有通过配置默认ip;
			local_port=1190;
			local_port2=1191;   //配置默认IP
			
			huart1_set.Instance = USART1;
			huart1_set.Init.BaudRate = 115200;
			huart1_set.Init.WordLength = UART_WORDLENGTH_8B;
			huart1_set.Init.StopBits = UART_STOPBITS_1;
			huart1_set.Init.Parity = UART_PARITY_NONE;
			huart1_set.Init.Mode = UART_MODE_TX_RX;
			huart1_set.Init.HwFlowCtl = UART_HWCONTROL_NONE;
			huart1_set.Init.OverSampling = UART_OVERSAMPLING_16;
			
			huart2_set.Instance = USART1;
			huart2_set.Init.BaudRate = 115200;
			huart2_set.Init.WordLength = UART_WORDLENGTH_8B;
			huart2_set.Init.StopBits = UART_STOPBITS_1;
			huart2_set.Init.Parity = UART_PARITY_NONE;
			huart2_set.Init.Mode = UART_MODE_TX_RX;
			huart2_set.Init.HwFlowCtl = UART_HWCONTROL_NONE;
			huart2_set.Init.OverSampling = UART_OVERSAMPLING_16;
		}
		

    return ;
}

void AT24C02_save_data(void)//保存数据到AT24C02
{
	  uint16_t CRC_CHECK;
	  CRC_CHECK=CRC_calculate_crc16(0xffff,0x8005,0,1,1,(uint8_t *)&http_at24_data,sizeof(http_at24_data));
	  AT24C02_WRITE((uint8_t *)&http_at24_data,0,sizeof(http_at24_data));
	  AT24C02_WRITE((uint8_t *)&CRC_CHECK,sizeof(http_at24_data),sizeof(CRC_CHECK));
}

//  huart1.Instance = USART1;
//  huart1.Init.BaudRate = 115200;
//  huart1.Init.WordLength = UART_WORDLENGTH_8B;
//  huart1.Init.StopBits = UART_STOPBITS_1;
//  huart1.Init.Parity = UART_PARITY_NONE;
//  huart1.Init.Mode = UART_MODE_TX_RX;
//  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
//  huart1.Init.OverSampling = UART_OVERSAMPLING_16;